#!/usr/bin/perl
#folderman-lib.pl

do "../web-lib.pl";
&init_config();
%access = get_module_acl();

sub check_path
{
	my $rst = 0;
	my $path = $_[0];
	my @checkpath=split(/\//, $path);
	$pathdeep=@checkpath;
	if(!($path =~ /^\//)){
		$rst = 0;
		return $rst;
	}
	$rst = 1;
	return $rst;
}
sub check_ip
{
	my $result = 0;
	my $ip = $_[0];
	my @checkip=split(/\./, $ip);
	$ipnum=@checkip;
	if(($ipnum !=4) || ($ip =~ /[\D]$/)){
		$result = 0;
		return $result;
	}
	my $i = 0;
	for ($i=0; $i<4; $i++) {
		if(($checkip[$i] =~ /\D/) || ($checkip[$i] > 255) || ($checkip[$i] eq '')){
			$result = 0;
			return $result;
		}
	}
	$result = 1;
	return $result;
}

sub get_nvinfo
{
	my @nvinfo;
	open(NV, "/etc/nas/netvoltab");
	my @nvtab = <NV>;
	close(NV);
	my $i = 0;
	foreach $nvt (@nvtab) {
		$nvt =~ s/\r|\n//g;
		if($nvt =~ /^(volume\d)\s+(\S+)\s+(\S+)/){
			$nvinfo[$i]->{'volname'} = $1;
			$nvinfo[$i]->{'volpath'} = $2;
			$nvinfo[$i]->{'stat'} = $3;
			$i++;		
		}
		else{
			next;
		}
		#$i++;
	}
	return @nvinfo;
}

sub get_free_netvol
{
	my @nvinfo = @_;
	my @freenv = ("volume1", "volume2", "volume3", "volume4", "volume5", "volume6");
	my @fnv=();
	my $fn;
	my $nv;
	foreach $fn (@freenv) {
		$found=0;
		foreach $nv (@nvinfo) {
			if($nv->{'volname'} eq $fn){
				$found = 1;
				last;
			}
		}
		if(!$found){
			push(@fnv, $fn);
		}
	}
	return @fnv;
}

sub del_netvoltab
{
        my ($volname, $line, @newtab);

        $volname = $_[0];

        my $voltab = $config{'storage_file'};
        open(VOLTAB, $voltab);
        while ($line = <VOLTAB>) {
                my $tmp = $line;
                $tmp =~ s/\r|\n//g;
                $tmp =~ s/#.*$//g;

                my @t;
                @t = split(/\s+/, $tmp);
                if ($volname eq $t[0]) {
                        next;
                }
                push (@newtab, $line);
        }
        close(VOLTAB);

        open(VOLTAB, "> $voltab");
        foreach $line (@newtab) {
                print VOLTAB $line;
        }
        close(VOLTAB);
}

sub add_netvoltab
{
        my ($volname, $line, @newtab);

        $volname = $_[0];
        $mddev = $_[1];
        $status = $_[2];

        my $voltab = $config{'storage_file'};
        open(VOLTAB, $voltab);
        while ($line = <VOLTAB>) {
                my $tmp = $line;
                $tmp =~ s/\r|\n//g;
                $tmp =~ s/#.*$//g;

                my @t;
                @t = split(/\s+/, $tmp);
                if ($volname eq $t[0]) {
                        next;
                }
                push (@newtab, $line);
        }
        close(VOLTAB);

        push (@newtab, join("\t", ($volname, $mddev, $status, "\n")));

        open(VOLTAB, "> $voltab");
        foreach $line (@newtab) {
                print VOLTAB $line;
        }
        close(VOLTAB);
}

sub get_mnt_root
{
	my $mntroot = "";
	open(STOR, $config{'storage_file'});
	while (<STOR>) {
		if (/^\s*volroot=\s*(\S*).*$/)
			{
				$mntroot = $1;
				break;
			}
	}
	close(STOR);
	return $mntroot;
}
sub get_netvol_info
{
	my $volname = $_[0];
	my @nvv;
	open(FG, $config{'storage_file'});
	my @nvinfo = <FG>;
	foreach $nv (@nvinfo){
		$nv =~ s/\n|\r//g;
		$nv =~ s/#//g;
		@nvv = split(" ", $nv);
		if($nvv[0] eq $volname) {
			return @nvv;
		}
		else{
			@nvv=();
			next;
		} 
	
	}
	close(FG);
	return @nvv;
}
sub vol_mount_status
{
        my (@p, @mstatus);

        @mstatus = (0, undef);
        open(MTAB, "/etc/mtab");
        while(<MTAB>) {
                chop;
                s/#.*$//g;
                if (!/\S/) { next; }
                @p = split(/\s+/, $_);
                if ($p[0] eq $_[0]) {
                        #found
                        $mstatus[0] = 1;
                        $mstatus[1] = $p[1];
                        return @mstatus;
                }
        }
        close(MTAB);
        return @mstatus;
}

sub list_folders
{
	my $dir = $_[0];
	my %tabfile;
	my (%htmltab, $linestr, $tmpline, $k);
	read_sharetab(\%tabfile, 2);
	if (!$dir)
	{
	        	
		print "<table width=100% class=mode2><tr>\n";
		print "<td valign=top><div class=main><table class=mode1 border=0 cellspacing=0 cellpadding=1 width=100%>\n";
		print "<tr class=title1> <th align=left width=25%>$text{'volumename'}</th> ",	
			"<th align=left width=20%>$text{'ip'}</th>",
			"<th align=left width=25%>$text{'sharepath'}</th> ",
			"<th align=left>$text{'nvstat'}</th> ",
			"<th align=left>$text{'auto'}</th> ",
			"</tr>\n";
		open(STOR, $config{'storage_file'});
		my $n = 0;
		my $mntroot;
		while (<STOR>) {
			if (/^\s*volroot=\s*(\S*).*$/)
			{
				$mntroot = $1;
				next;
			}
			elsif (/^\s*#/)
			{
				next;
			}
			elsif (/^\s*(\S+)\s+(\S*)\s+(\S*).*$/)
			{
				$pname = $1;
				$dev = $2;
				$mntstatus = $3;
				if($mntstatus){
					$autotext=$text{'auto1'};
				}
				else{
					$autotext=$text{'auto0'}
				}
				$dev =~ /^(\S+):(\/\S+)/;
				$ip = $1;
				$share=$2;
				#next if ($mntstatus !~ /^normal$/);
			}
			else
			{
				next;
			}
			$path = "$mntroot$pname";
			@mountstat=vol_mount_status($dev);
			if (!(-d $path)) { next; }
			$n++;
			if ($n % 2 eq 0)
			{
				$dismode = "class=mode2";

			}
			else
			{
				$dismode = "class=mode1";

			}
#			print "<tr $dismode>\n";
			$linestr = "<tr $dismode>\n";
			my $icon = "../images/dir.gif";
			my $link;
			if($mountstat[0]){
				$link = "<a href=\"\" onclick='enter_dir(\"$path/\"); return false'>";
			}
			else{
				$link = "<a href=\"\">";
			}
#			print "<td>", $link, "<img border=0 src=$icon>$pname</a></td> ";
			$linestr .= "<td><input type=radio name=volname value=$pname>" . $link . "<img border=0 src=$icon>" . $pname . "</a></td> ";
			my @st = stat($path);
			my @tm = localtime($st[9]);
#			printf "<td nowarp $dismode>%.2d/%s/%.4d %.2d:%.2d</td>\n", $tm[3], $text{'smonth_'.($tm[4]+1)}, $tm[5]+1900, $tm[2], $tm[1];
			#$tmpline = sprintf "<td nowarp $dismode>%.2d/%s/%.4d %.2d:%.2d</td>\n", $tm[3], $text{'smonth_'.($tm[4]+1)}, $tm[5]+1900, $tm[2], $tm[1];
			$tmpline = sprintf "<td nowarp $dismode>$ip</td>";
			$linestr .= $tmpline;
			my $attr = '';
			$attr .='R ' if ($st[2] & 0004);
			$attr .='W ' if ($st[2] & 0002);
			$attr .='E ' if ($st[2] & 0001);
			$attr = $text{'none'} if (!$attr);
			
			$tmpline = sprintf "<td nowarp>$share</td>";
			$linestr .= $tmpline;	
			#@mountstat=vol_mount_status($dev);
			if($mountstat[0]){
				$connstat=$text{'mounted'};
			}
			else{
				$connstat=$text{'umounted'};
			}
			$tmpline = sprintf "<td nowarp>$connstat", &html_escape($attr), "</td>\n";
			$linestr .= $tmpline;

                        $tmpline = sprintf "<td nowarp>$autotext</td>";
                        $linestr .= $tmpline;

			$linestr .= "</tr>\n";
			$htmltab{$pname} = $linestr;
		}
		close(STOR);

		foreach $k (sort keys %htmltab)
		{
			print $htmltab{$k};
		}

		print "</table></div></td>\n";
		print "<td valign=top><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
			print "<tr><td align=center><input type=submit name=createnfs value=\"$text{'btn_create'}\" onclick='return SetNvEditName(this, $n)' class=button></td></tr>\n";
		if ($n)
		{
			print "<tr><td align=center><input type=submit name=connectnetvol value=\"$text{'btn_conn'}\" onclick='return SetNvEditName(this, $n)' class=button></td></tr>";
                        #print "<tr><td align=center><input type=submit name=disconnetvol value=\"Disconnect Net Folder\" onclick='return SetNvEditName(this, $n)' class=button></td></tr>";
                        print "<tr><td align=center><input type=submit name=deletenetvol value=\"$text{'btn_delete'}\" onclick='return SetNvEditName(this, $n)' class=button></td></tr>";
                        #print "<tr><td align=center><input></td></tr>";
		}
		print "</table></td>";
		print "</tr>\n";
		print "<tr><td colspan=2><i>$text{'attributes_hint'}</i></td></tr>\n";
		print "<tr><td colspan=2><i>$text{'sharetypes_hint'}</i></td></tr>\n";
		print "</table>\n";
		return $n;
	}

	$dir =~ /^(.*\/)[^\/]+\/$/;


	print "<table width=100% class=mode2><tr>\n";
	print "<td valign=top><div class=main><table class=mode1 border=0 bordercolor=#000000 cellspacing=0 cellpadding=1 width=100%>\n";
	print "<tr><td><b>", &text('choose_dir', $dir), "</b></td><td colspan=3 align=right><a href=\"\" onclick='enter_dir(\"$1\"); return false'>$text{'parentdir'}</a></td></tr>\n";
	print "<tr class=title1> <th align=left>$text{'foldername'}</th> ",
		"<th align=left width=25%>$text{'date'}</th> ",
		"<th align=left width=15%>$text{'attributes'}</th> ",
		"<th align=left width=15%>$text{'sharetype'}</th> </tr>\n";


	opendir(DIR, $dir);
	my $n = 0;
	foreach $f (sort {$a cmp $b} readdir(DIR)){
		my $path = "$dir$f";
		if ($f eq "." || $f eq "..") { next; }
		if (!(-d $path)) { next; }
		if($f eq $config{'lost+found'})	{ next;	}
		$n++;
		#print "<tr $cb>\n";
		if ($n % 2 eq 0)
		{
			$dismode = "class=mode2";

		}
		else
		{
			$dismode = "class=mode1";

		}



		print "<tr $dismode>\n";
		my $icon = "../images/dir.gif";
		my $link = "<a href=\"\" onclick='enter_dir(\"$path/\"); return false'>";
		print "<td><input type=radio name=subdir value='$path'>$link<img border=0 src=$icon>$f</a></td> ";
		my @st = stat($path);
		my @tm = localtime($st[9]);
		printf "<td nowarp $dismode>%.2d/%s/%.4d %.2d:%.2d</td>\n", $tm[3], $text{'smonth_'.($tm[4]+1)}, $tm[5]+1900, $tm[2], $tm[1];
		my $attr = '';
		$attr .='R ' if ($st[2] & 0004);
		$attr .='W ' if ($st[2] & 0002);
		$attr .='E ' if ($st[2] & 0001);
		$attr = $text{'none'} if (!$attr);

		print "<td nowarp>", &html_escape($attr), "</td>\n";
		if (exists($tabfile{$path}))
		{
			my $stype;
			$stype .='W ' if ($tabfile{$path}{'sharetype'} =~ /W/);
			$stype .='U ' if ($tabfile{$path}{'sharetype'} =~ /U/);
			$stype .='F ' if ($tabfile{$path}{'sharetype'} =~ /F/);
			$stype .='H ' if ($tabfile{$path}{'sharetype'} =~ /H/);
			$stype .='A ' if ($tabfile{$path}{'sharetype'} =~ /A/);
			print "<td nowarp $dismode>$stype</td>\n";
		}
		else
		{
			print "<td nowarp $dismode>$text{'none'}</td>\n";
		}
		print "</tr>\n";
	}
	closedir(DIR);
	print "<tr><td colspan=4  align=right><a href=\"\" onclick='enter_dir(\"$1\"); return false'>$text{'parentdir'}</a></td></tr>\n";
	print "</table></div></td>\n";
	print "<td width=160 valign=top><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
	print "<tr><td align=center><input type=submit name=newdir value=\"$text{'create_folder'}\" onclick='return SetEditName(this, $n)' class=button></td></tr>\n";
#	if ($n)
#	{
	printf "<tr><td align=center><input type=submit %s name=deldir value=\"$text{'delete_folder'}\" onclick='return SetEditName(this, $n)' class=button></td></tr>\n", ($n)?"":"disabled=true";
	printf "<tr><td align=center><input type=submit %s name=editdir value=\"$text{'folder_property'}\" onclick='return SetEditName(this, $n)' class=button></td></tr>\n", ($n)?"":"disabled=true";
	printf "<tr><td align=center><input type=submit %s name=sharedir value=\"$text{'share_property'}\" onclick='return SetEditName(this, $n)' class=button></td></tr>\n", ($n)?"":"disabled=true";
#	}
	print "</table></td> </tr>\n";
	print "<tr><td colspan=2><i>$text{'attributes_hint'}</i></td></tr>\n";
	print "<tr><td colspan=2><i>$text{'sharetypes_hint'}</i></td></tr>\n";
	print "</table>\n";

#	print "</form>\n";
	#print "<a href=index.cgi?dir=$1>", $text{'parentdir'}, "</a>\n";
	return $n;
}

#the 2nd parameter: 1--key is sharename; 2--key is sharepath
sub read_sharetab
{
	local $a = $_[0];
	local $k = $_[1];
	open(SHARETAB, $config{'sharetab_file'});
	while($line = <SHARETAB>)
	{
		$line =~ s/\r|\n//g;
		$line =~ s/#.*$//g;
		$line =~ s/\/\/.*$//g;

		if ($line)
		{
			while($line=~ /\\$/)
			{
				local $nl = <SHARETAB>;
				$line =~ s/\\$//;
				$nl =~ s/^\s+//;
				$line.=$nl;
			}
			local ($path, $sharename, $sharetype, $comment) = split(/:/, $line);
			$a->{$sharename} = {sharepath => $path, sharetype => $sharetype, comment => $comment} if ($k ==1);
			$a->{$path} = {sharename => $sharename, sharetype => $sharetype, comment => $comment} if ($k ==2);
		}

	}
	close(SHARETAB);
}

1;
